import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';
import 'package:gluz/app/common/common_button.dart';
import 'package:keyboard_actions/keyboard_actions.dart';

import '../../../../app_session.dart';
import '../../../utils/colors.dart';
import '../controllers/login_controller.dart';

class LoginView extends GetView<LoginController> {
  const LoginView({super.key});
  @override
  // TODO: implement controller
  // LoginController get controller => Get.put(LoginController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GetBuilder<LoginController>(
        builder: (controller) {
          return SingleChildScrollView(
            child: Stack(
              children: [
                Positioned(
                  top: 0,
                  left: 0,
                  right: 0,
                  child: Image.asset('assets/images/login_top_gradient.png',),
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const SizedBox(height: 100),
                      _buildSlogan(),
                      const SizedBox(height: 30),
                      _buildLanguageSelector(),
                      const SizedBox(height: 20),
                      _buildPhoneInput(),
                      const SizedBox(height: 10),
                      _buildPasswordInput(),
                      const SizedBox(height: 20),
                      _buildAgreementCheckbox(),
                      const SizedBox(height: 20),
                      _buildLoginButton(),
                      const SizedBox(height: 10),
                      _buildLinks(),
                      const SizedBox(height: 40),
                      _buildRegisterLink(),
                    ],
                  ),
                ),
                _buildLogoSection(),
              ],
            ),
          );
        }
      ),
    );
  }

  Widget _buildSlogan() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        ShaderMask(
          shaderCallback: (Rect bounds) {
            return LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                AppColors.color_66D6E2,
                AppColors.color_278AFF,
              ],
            ).createShader(bounds);
          },
          blendMode: BlendMode.srcATop,
          child: Text(
            'GULZ',
            style: TextStyle(
              fontSize: 24,
              fontWeight: FontWeight.bold,
              color: AppColors.color_278AFF,
            ),
          ),
        ),

        const SizedBox(height: 10,),
        SizedBox(
          height: 35,
          child: Text(
            '跨越山海 链接未来'.tr,
            style: TextStyle(
              fontSize: 12,
              color: Colors.black,
            ),
          ),
        ),
      ],
    );
  }

  /// 构建顶部Logo和应用名称区域
  Widget _buildLogoSection() {
    return Positioned(
      top: 72,
      right: 20,
      width: 126,
      height: 126,
      child: Image.asset('assets/images/login_logo.png'),
    );
  }

  /// 构建语言选择按钮区域
  Widget _buildLanguageSelector() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        SizedBox(
          height: 24,
          width: 75,
          child: app.isLangRU() ? GLOutLineButton(
              onPressed: controller.onChineseLanguageSelected,
              child: Text('中文'.tr, style: TextStyle(color: AppColors.color_8C8C8C, fontSize: 13),)
          ) : GLGradientButton(
            onPressed: controller.onChineseLanguageSelected,
            child: Text('中文'.tr, style: TextStyle(color: Colors.white, fontSize: 13),)
          ),
        ),
        const SizedBox(width: 10),
        SizedBox(
          height: 24,
          width: 75,
          child: app.isLangRU() ? GLGradientButton(
            onPressed: controller.onRussianLanguageSelected,
            child: Text('RU'.tr, style: TextStyle(color: Colors.white, fontSize: 13),)
          ) : GLOutLineButton(
              onPressed: controller.onRussianLanguageSelected,
              child: Text('RU'.tr, style: TextStyle(color: AppColors.color_8C8C8C, fontSize: 13),)
          ),
        ),
      ],
    );
  }

  /// 构建手机号输入框
  Widget _buildPhoneInput() {
    return TextField(
      controller: controller.phoneController,
      keyboardType: TextInputType.phone,
      cursorColor: AppColors.color_278AFF,
      decoration: InputDecoration(
        prefixIcon: InkWell(
          onTap: controller.onTapCountry,
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(
                width: 20,
                height: 13,
                child: Image.asset(controller.selectedCountry.image),
              ),
              SizedBox(width: 3),
              Text('+${controller.selectedCountry.code}', style: TextStyle(fontSize: 14),),
              Icon(Icons.keyboard_arrow_down_rounded, color: Colors.black,),
            ],
          ),
        ),
        hintText: '请输入手机号'.tr,
        hintStyle: TextStyle(color: AppColors.color_D8D8D8, fontSize: 14),
        enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: AppColors.color_D8D8D8, width: 0.5)
        ),
        focusedBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: AppColors.color_D8D8D8, width: 0.5)
        ),
      ),
    );
  }

  /// 构建密码输入框
  Widget _buildPasswordInput() {
    return TextField(
      controller: controller.pwdController,
      obscureText: true,
      keyboardType: TextInputType.visiblePassword,
      cursorColor: AppColors.color_278AFF,
      decoration: InputDecoration(
        prefixIcon: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text('密码'.tr, style: TextStyle(fontSize: 15),),
            SizedBox(width: 3),
          ],
        ),
        hintText: '请输入密码'.tr,
        hintStyle: TextStyle(color: AppColors.color_D8D8D8, fontSize: 14),
        enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: AppColors.color_D8D8D8, width: 0.5)
        ),
        focusedBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: AppColors.color_D8D8D8, width: 0.5)
        ),
      ),
    );
  }

  /// 构建同意协议复选框
  Widget _buildAgreementCheckbox() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Transform.scale(
          scale: 0.8,
          child: SizedBox(
            width: 18,
            height: 18,
            child: Checkbox(
              activeColor: AppColors.color_278AFF,
              value: controller.isCheckLink,
              onChanged: (value) {
                controller.isCheckLink = !controller.isCheckLink;
                controller.update();
              },
            ),
          ),
        ),
        SizedBox(width: 5,),
        Expanded(
          child: RichText(
            text: TextSpan(
              text: '阅读并同意'.tr,
              style: TextStyle(color: Colors.black, fontSize: 12),
              children: [
                TextSpan(
                  text: '《GULZ用户服务协议和隐私政策》'.tr,
                  style: TextStyle(color: AppColors.color_16C186),
                  recognizer: TapGestureRecognizer()
                    ..onTap = controller.onUserAgreementTapped,
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  /// 构建登录按钮
  Widget _buildLoginButton() {
    return GLGradientButton(
      onPressed: controller.onLoginButtonPressed,
      child: Text('登录'.tr, style: TextStyle(
        color: Colors.white, 
        fontSize: 16, 
        fontWeight: FontWeight.bold
      ),)
    );
  }

  /// 构建辅助链接（忘记密码/验证码登录）
  Widget _buildLinks() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        TextButton(
          onPressed: controller.onForgotPasswordPressed,
          style: TextButton.styleFrom(padding: EdgeInsets.zero),
          child: Text('忘记密码'.tr, style: TextStyle(color: AppColors.color_333333, fontSize: 12),),
        ),
        TextButton(
          onPressed: controller.onVerificationCodeLoginPressed,
          child: const Text(''),
        ),
      ],
    );
  }

  /// 构建注册链接
  Widget _buildRegisterLink() {
    return Center(
      child: InkWell(
        onTap: controller.onRegisterLinkTapped,
        child: Container(
          padding: EdgeInsets.all(15),
          child: RichText(
            text: TextSpan(
              text: '没有账号？'.tr,
              style: TextStyle(color: Colors.black),
              children: [
                TextSpan(
                  text: '立即注册'.tr,
                  style: TextStyle(
                    color: AppColors.color_16C186,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
